package com.calcs.logger
{
	import com.adobe.air.logging.FileTarget;
	
	import flash.filesystem.*;
	
	import mx.logging.*;
	
	public class Logger
    {
        //~ Instance Attributes -----------------------------------------------
        private var logger : ILogger;
		private var isLoggerInitialized:Boolean = false;
		
        //~ Constructor -------------------------------------------------------
        /**
         * SingletonEnforcer parameter ensures that only the getInstance
         * method can create an instance of this class.
         */
        public function Logger(category : String, enforcer : SingletonEnforcer) : void
        {
            this.logger = Log.getLogger(category);
            if(!isLoggerInitialized)
            {
				var logTargetFile:File = new File("C:\\Application Logs\\Log.log");
				var loggingTarget:ILoggingTarget = new FileTarget(logTargetFile);
	            // Log all log levels.
	            loggingTarget.level = LogEventLevel.ALL;
	            

	            // Add date, time, category, and log level to the output.
	            /*this.logger.includeDate = true;
	            this.logger.includeTime = true;
	            this.logger.includeCategory = true;
	            this.logger.includeLevel = true;*/
	
	            // Begin logging.
	            //Log.addTarget(logTarget);
	            Logger.addTarget(loggingTarget);
	            isLoggerInitialized = true;
            }
        }

        //~ Methods -----------------------------------------------------------
        public static function getInstance(category : String) : Logger
        {
            return new Logger(category, new SingletonEnforcer());
        }

        public static function addTarget(target : ILoggingTarget) : void
        {
            Log.addTarget(target);
        }

        public static function removeTarget(target : ILoggingTarget) : void
        {
            Log.removeTarget(target);
        }

        public function debug(message : String) : void
        {
            this.logger.debug(message);
        }
		
		public function warn(message: String):void
		{
			this.logger.warn(message);
		}
		
		public function error(message : Object):void
		{
			if(Object instanceof String)
			{
				var errorMessage1:String = String(message); 
				this.logger.error(errorMessage1);
			}
			else if(Object instanceof Error)
				var errorMessage2:Error = Error(message);
				this.logger.error(errorMessage2.getStackTrace());	
		}
        // other logging methods by level (e.g. warn, error)

    }
}
class SingletonEnforcer {}